<?php

namespace App\Models\Wanggui;

use App\Models\BaseModel;
use App\Models\Kms\User\KmsUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class CustomerDepartmentOperationLogModel extends BaseModel
{
    use HasFactory;

    protected $table = 'customer_department_operation_log';

    protected $fillable = [
        'taskId',
        'type',
        'content',
        'changes',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];


    /**
     * 添加日志
     * @param int $taskId
     * @param string $content
     * @param array $changes
     * @param int $userId
     * @return bool
     */
    public static function addLog(int $taskId, string $content, array $changes, int $userId): bool
    {
        return self::query()->insert([
            'taskId'       => $taskId,
            'content'      => $content,
            'changes'      => empty($changes) ? '' : json_encode($changes, JSON_UNESCAPED_UNICODE),
            'createdByUid' => $userId,
            'updatedByUid' => $userId,
            'createdAt'    => time(),
            'updatedAt'    => time(),
        ]);
    }


    public function userRel(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne(KmsUser::class, 'rmsUserId', 'createdByUid');
    }
}
